<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="tephone=no,email=no,date=no,address=no">
		<style type="text/css">
			html body {
				background-color: white;
			}
			button {
				padding: 5px 10px;
				margin: 10px;
				display: inline-block;
				background-color: #e8e8e8;
			}
		</style>
	</head>
	<body>
		<header>
			<h1>蓝牙电子秤</h1>
			<mark>
                连接电子秤测试
			</mark>
			<hr />
			<br />
		</header>
        <button onclick="initBle()">初始化蓝牙插件</button>
        <br />
		<button onclick="scanDevice()">开始扫描周围的蓝牙设备</button>
        <br />
        <button onclick="getDeviceInfo()">获取扫描到的蓝牙设备</button>
        <br />
        <button onclick="connectTarget()">链接目标设备</button>
        <br />
        <button onclick="fnCloseFrame()"> 关闭本页</button>

	</body>
	<script type="text/javascript">
        
         var allperipheral;
          
        function initBle() {
            var ble = api.require('ble');
            ble.initManager({
                single:false
            },function(ret) {
                api.alert({ msg: JSON.stringify(ret) });
            });
        }
        function scanDevice() {
            var ble = api.require('ble');
            ble.scan({
            
            },function(ret) {
                if (ret.status) {
                    alert('开始扫描');
                }
            });
        };
        
       function getDeviceInfo () {
           var ble = api.require('ble');
           ble.getPeripheral(function(ret) {
               if (ret) {
               //api.alert({ msg: JSON.stringify(ret) });
               
               allperipheral = ret.peripherals;
               var uuidStr;
               const tName = "BT04-A";
               for (let element of allperipheral) {
                   let pName = element.advertisingName;
                 // 遍历数组，对每个元素进行操作
                 if (startsWith(pName,tName)) {
                     uuidStr = element.uuid;
                     break;
                 }
               }
               api.alert({msg:"获取到的uuid是："+uuidStr});
               }
           });
       }
        function connectTarget() {
            var uuidStr;
            const tName = "BT04-A";
            for (let element of allperipheral) {
                let pName = element.advertisingName;
              // 遍历数组，对每个元素进行操作
              if (startsWith(pName,tName)) {
                  uuidStr = element.uuid;
                  break;
              }
            }
            
            var ble = api.require('ble');
            ble.connect({
                peripheralUUID: uuidStr
            }, function(ret, err) {
                if (ret.status) {
                    alert("连接成功！");
                } else {
                    api.alert({msg:err.code+":"+uuidStr});
                }
            });
        }
       
       function fnCloseFrame(){
           api.closeFrame({
               name: 'bleEleScale'
           });
       }
        function startsWith(str, prefix) {
          return str.indexOf(prefix) == 0;
        }
        
        
       
        
	</script>
</html>
